//
//  BusinesActivityCell.swift
//  XiaoJingMallSwift
//
//  Created by beisheng03 on 2017/8/4.
//  Copyright © 2017年 beisheng. All rights reserved.
//

import UIKit
import SDWebImage

class BusinesActivityCell: BaseTableViewCell {
    
    @IBOutlet weak var topImageView: UIImageView!
    
    @IBOutlet weak var imageTopLabel: UILabel!
    
    @IBOutlet weak var imageViewH: NSLayoutConstraint!
    
    @IBOutlet weak var bottomMagin: NSLayoutConstraint!
    @IBOutlet weak var middleLabel: UILabel!
    
    @IBOutlet weak var leftLabel: UILabel!
    
    @IBOutlet weak var rightLabel: UILabel!
    @IBOutlet weak var lineView: UIView!
    
    var model : BusinessActivityModel? {
        
        didSet{
            
            if model == nil {
                return
            }
            
            if let imageCach = SDWebImageManager.shared().imageCache {
                
                if let tempImage = imageCach.imageFromDiskCache(forKey: model?.article_cover)  {
                    
                    imageViewH.constant = tempImage.size.height / tempImage.size.width * (KScreenW - 20)
                    
                    
                    topImageView.image = tempImage
                    
                }else{
                    
                    imageViewH.constant = (KScreenW - 20) / 16 * 9
                    
                    topImageView.sd_setImage(with: URL(string :model!.article_cover), placeholderImage: UIImage.defaulImage(), options: .progressiveDownload) { (image, _, _, _) in
                        
                    }
                    
                }
                
                
                
            }else{
                
                
                imageViewH.constant = (KScreenW - 20) / 16 * 9
                
                topImageView.sd_setImage(with: URL(string :model!.article_cover), placeholderImage: UIImage.defaulImage(), options: .progressiveDownload) { (image, _, _, _) in
                    
                }
                
                
            }
            
            
            
            imageTopLabel.text = model?.article_title
            
            middleLabel.text = model?.article_desc
            
            leftLabel.attributedText = attributedString(str: "活动时间: \(model!.start_time)至\(model!.end_time)", font: UIFont.systemFont(ofSize: 13))
            
            
            rightLabel.attributedText = attributedString(str: "阅读量: \(model!.article_view)次", font: UIFont.systemFont(ofSize: 13))
            
            
            
        }
        
    }

    override func awakeFromNib() {
        super.awakeFromNib()
        lineView.backgroundColor = UIColor.viewBgGrey()
        
        bottomMagin.constant = 0
        
        middleLabel.textColor = UIColor.black
//        
//        leftLabel.textColor = UIColor.dakGray()
//        
//        rightLabel.textColor = UIColor.dakGray()
        
        contentView.backgroundColor = UIColor.white
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }
    
}



extension BusinesActivityCell{


    func attributedString(str: String, font: UIFont) -> NSAttributedString? {
        
        
        let patten0 = "[0-9]{1,15}"
        //创建正则表达式对象
        let patten1 = "[-]"
        
        //        let patten2 = "[¥]"    + "|" +  patten2
        
        let patten = patten0 + "|" + patten1
        
        //caseInsensitive  忽略大小写  最常用
        //dotMatchesLineSeparators 允许 . 匹配任意字符，包括回车换行
        let regex = try! NSRegularExpression(pattern: patten, options: NSRegularExpression.Options.caseInsensitive)
        
        //根据规则匹配指定字符串中第一个符合规则的字符串，并且返回一个NSTextCheckingResult
        let res1 = regex.matches(in: str, options: NSRegularExpression.MatchingOptions.init(rawValue: 0), range: NSRange(location: 0, length: str.characters.count))
        
        // 根据原有字符串生成属性字符串
        let strM = NSMutableAttributedString(string: str)
        
        
        strM.addAttribute(NSForegroundColorAttributeName, value: UIColor.dakGray(), range:NSMakeRange(0, str.characters.count) )
        
        strM.addAttribute(NSFontAttributeName, value: font, range:NSMakeRange(0, str.characters.count))
        
        
        for temp in res1{
            
            
            strM.addAttribute(NSForegroundColorAttributeName, value: UIColor.themeColor(), range:temp.range )
            
            
            
            
        }
        
        
        
        
        return strM
        
        
        
        
        
        
    }








}
